<?php
/**
 * Created by PhpStorm.
 * User: 张良
 * Date: 2018/1/11
 * Time: 18:15
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\CommonController;
use App\Models\Admin;
use App\Models\FirstType;
use App\Models\Product;
use App\Models\Ipsell;
use App\Models\Progress;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Request;
use Redirect;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

class GeneralAdminController extends CommonController
{
    protected  $pageNum=15;
    protected $patent = 1;
    protected $trademark = 2;
    protected $copyright = 3;
    protected $project = 4;
    public function login()
    {
        return view( 'generalAdmin/login' );
    }

    public function doLogin()
    {
        $input    = Request::all();
        $account  = $input[ 'account' ];
        $password = md5( $input[ 'password' ] );
        $user=Admin::where(['type'=>0])->first();
        if(empty($user)){
            $user['account']='admin';
            $user['password']=md5('123456');
        }
        if ($account == $user['account'] && $password == $user['password']) {
            return Redirect::to( 'Admin/generalAdmin/index' );
        }
        else{
            return back()->with( 'error','用户名或密码错误' );
        }
    }

    public function index()
    {
       $input=Request::all();
       if(!empty($input)){
           $Year=$input['checkYear'];
           $Month=$input['checkMonth'];
           $data = $this->getSalesAnalysis($Year,$Month);
       }else{
           $data = $this->getSalesAnalysis();
       }
        $count=$this->getData();
        $result=Db::table('progress')->where([
            ['deleted_at','=',null],
            ['state','=','0']
        ])->count();
        $sql=Db::table('progress')->where([
            ['deleted_at','=',null],
            ['state','=','1']
        ])->count();
        return view( 'generalAdmin/index',compact(['count','result','sql']) )->with( 'data',$data );
    }
    public function getData(){
        $count=[0,0,0,0];
        $num=[0,0,0,0];
        $product=Progress::all();
        $date=[
            "start"=>0,
            "end"=>0
        ];
        for($i=0,$size=count($product);$i< $size;$i++){
            if($i==0){
                $date['start']=date("Y",strtotime($product[$i]['created_at']));
            }
            if($i==$size-1){
                $date['end']=date("Y",strtotime($product[$i]['created_at']));
            }
            $index=$product[$i]['first_type_id']-1;
            if(!empty($product[$i]['tail_pay_time'])){
                $num[$index]++;
            }
            $count[$index]++;
        }
        $data["count"]=$count;
        $data["num"]=$num;
        $data["date"]=$date;
        return $data;
    }
    public function getSalesAnalysis($Year = null,$Month = null)
    {
       
        $timeZone = "Asia/Shanghai"; //时区
        //  $dayOfMonth=[31,[28,29],31,30,31,30,31,31,30,31,30,31];
        if (empty( $Year )){  //默认当年
            $Year = (int)date( "Y",time() );
        }
        if (empty( $Month )){  //默认当月
            $Month = (int)date( "n",time() );
        }
        $YearCarbon = Carbon::create( $Year,$Month,1,0,0,0,$timeZone );
        //初始化时间
        $isLeapYear = $YearCarbon->isLeapYear();
        //判断是否为闰年
        $dayOfMonthNum = $YearCarbon->daysInMonth;
        //获取该月有多少天
        for ($i = 0;$i < 12;$i++){
            $defaultMonthData[ $i ] = 0;
        }
        //初始化月销售额
        for ($i = 0;$i < $dayOfMonthNum;$i++){
            $defaultDayData[ $i ] = 0;
        }
        //初始日销售额
        $yearData = [
            "startTime" => "",
            "endTime"   => "",
        ];
        //初始年开始结束时间
        $monthData = [
            "startTime" => "",
            "endTime"   => "",
        ];
        //初始月开始结束时间
        $yearData[ 'startTime' ] = $YearCarbon->lastOfYear()->startOfYear()->timezone( 'Asia/Shanghai' )->format( 'Y-m-d H:i:s' );
        $yearData[ 'endTime' ]   = $YearCarbon->lastOfYear()->endOfYear()->timezone( 'Asia/Shanghai' )->format( 'Y-m-d H:i:s' );
        //获取年开始结束时间
        $MonthCarbon = Carbon::create( $Year,$Month,1,0,0,0,$timeZone );

        $monthData[ 'startTime' ] = $MonthCarbon->startOfMonth()->timezone( 'Asia/Shanghai' )->format( 'Y-m-d H:i:s' );
        $monthData[ 'endTime' ]   = $MonthCarbon->endOfMonth()->timezone( 'Asia/Shanghai' )->format( 'Y-m-d H:i:s' );
        //获取月开始结束时间
        $where_process = [
            [ "created_at",">",$yearData[ 'startTime' ] ],
            [ "created_at","<",$yearData[ 'endTime' ] ],
        ];
        $with_with     = [ "FirstType" ];
        $processModel  = Progress::with( $with_with )->where( $where_process )->get();
        //查询该年交易
        if (!empty( $processModel )){
            $process   = $processModel->toArray();
            $newCarbon = new Carbon();
            $first     = $newCarbon->parse( $monthData[ 'startTime' ],$timeZone );
            $second    = $newCarbon->parse( $monthData[ 'endTime' ],$timeZone );

            //创建月开始结束时间  （没用）
            foreach ($process as $key => $value){
               $first_payment=(int)$value[ 'first_payment' ];
               $tail_payment=(int)$value[ 'tail_payment' ];
                if ((!empty( $value[ 'first_pay_time' ] ))&&checkNum($first_payment)){ //首款支付
                    $first_pay_time_checkTime = $newCarbon->parse( $value[ 'first_pay_time' ],$timeZone );

                    $first_pay_time_month = $first_pay_time_checkTime->month;
                    //获取首款支付月份
                    $defaultMonthData[ $first_pay_time_month - 1 ] += $first_payment;

                    if ((int)$first_pay_time_month == $Month){ //是否是该月
                        $val_day                        = $first_pay_time_checkTime->day;
                        $defaultDayData[ $val_day - 1 ] += $first_payment;
                    }
                    if ((!empty( $value[ 'tail_pay_time' ] ))&&checkNum($tail_payment)){ //尾款付清

                        $tail_pay_time_checkTime = $newCarbon->parse( $value[ 'tail_pay_time' ],$timeZone );

                        $tail_pay_time_month = $tail_pay_time_checkTime->month;
                        //获取尾款支付月份
                        $defaultMonthData[ $tail_pay_time_month - 1 ] += $tail_payment;

                        if ((int)$tail_pay_time_month == $Month){//是否是该月
                            $val_day                        = $tail_pay_time_checkTime->day;
                            $defaultDayData[ $val_day - 1 ] += $tail_payment;
                        }

                    }
                }
            }
        }
        //格式化
        $data = [
            'yearSell'  => implode( ";",$defaultMonthData ),
            'monthSell' => implode( ";",$defaultDayData ),
            'year'      => $Year,
            'month'     => $Month,
        ];
        //TODO  如果需要  做缓存。
        return $data;
    }


    public function staff()
    {
        $staff=Admin::whereIn('type',[1,2,3,4,9])->paginate($this->pageNum);
        $data=['staff'];
        return view( 'generalAdmin/staff' ,compact($data));
    }
//    public function staffSearch()
//    {
//        $request = Request::all();
//        $keywords="";
//        if (isset($request['search'])) {
//            $keywords = $request['search'];
//        }
//        $staff=Admin::whereIn('type',[1,2,3,4])->paginate($this->pageNum);
//        $data=['staff','keywords'];
//        return view( 'generalAdmin/staffSearch' ,compact($data));
//    }

    public function staffCertainSearch()
    {
        $type = Request::input('type');
        $duty = Request::input('duty');
        $real_name = Request::input('real_name');
        $account = Request::input('account');
        $phone = Request::input('phone');
        $certificate_number = Request::input('certificate_number');
        $email = Request::input('email');
        $hiredate = Request::input('hiredate');
        $staff=Admin::whereIn('type',[1,2,3,4,9])
            ->when($type, function ($query) use ($type) {
                return $query->where('type', $type);
            })
            ->when($duty, function ($query) use ($duty) {
                return $query->where('duty', $duty);
            })
            ->when($real_name, function ($query) use ($real_name) {
                return $query->where('real_name', $real_name);
            })
            ->when($account, function ($query) use ($account) {
                return $query->where('account', $account);
            })
            ->when($phone, function ($query) use ($phone) {
                return $query->where('phone', $phone);
            })
            ->when($certificate_number, function ($query) use ($certificate_number) {
                return $query->where('certificate_number', $certificate_number);
            })
            ->when($email, function ($query) use ($email) {
                return $query->where('email', $email);
            })
            ->when($hiredate, function ($query) use ($hiredate) {
                return $query->where('hiredate','like','%'.$hiredate.'%');
            })
            ->paginate($this->pageNum);
        $data=['staff'];
        return view( 'generalAdmin/staffSearch' ,compact($data));
    }
    public function add_staff()
    {
        $input                        = Request::all();
        $data[ 'password' ]           = md5( '123456' );
        $data[ 'type' ]               = $input[ 'department' ];
        $data[ 'duty' ]               = $input[ 'post' ];
        $data[ 'account' ]            = $input[ 'account' ];
        $data[ 'certificate_number' ] = $input[ 'card' ];
        $data[ 'phone' ]              = $input[ 'tel' ];
        $data[ 'email' ]              = $input[ 'email' ];
        $data[ 'hiredate' ]           = $input[ 'apply_date' ];
        $data[ 'created_at' ]         = date( 'Y-m-d h:i:s' );
        $data[ 'updated_at' ]         = date( 'Y-m-d h:i:s' );
        $data[ 'real_name' ]          = $input[ 'name' ];
        $check                        = object_to_array( DB::table( 'admin' )->where( [ "account" => $data[ 'account' ] ] )->first() );
        if ($check){
            return back()->with( 'error','该用户名已被注册' );
        }
        else{
            $res = DB::table( 'admin' )->insertGetId( $data );
            if ($res){
                return back()->with( 'success','添加成功' );
            }
            else{
                return back()->with( 'error','添加失败' );
            }
        }
    }

    public function del_staff($id)
    {
        $check = object_to_array( DB::table( 'admin' )->where( [ "id" => $id ] )->first() );
        if (!empty( $check )){
            $res = DB::table( 'admin' )->where( [ "id" => $id ] )->delete();
            if ($res){
                return back()->with( 'success','删除成功' );
            }
            else{
                return back()->with( 'error','删除失败' );
            }
        }
        else{
            return back()->with( 'error','查无此数据' );
        }
    }

    public function reset_pwd($id)
    {
        //    dump($id);exit();
        $check = object_to_array( DB::table( 'admin' )->where( [ "id" => $id ] )->first() );
        if (!empty( $check )){
            $data[ 'password' ] = md5( '123456' );
            $res                = DB::table( 'admin' )->where( [ "id" => $id ] )->update( $data );

            if ($res){
                return back()->with( 'success','密码重置成功' );
            }elseif($res==0){
                return back()->with( 'error','与原密码相同，无需重置' );
            }
            else{
                return back()->with( 'error','密码重置失败' );
            }
        }
        else{
            return back()->with( 'error','查无此数据' );
        }

    }

    public function edit_staff()
    {
        $input                        = Request::all();
        $id                           = $input[ 'hide_id' ];
        $data[ 'type' ]               = $input[ 'department' ];
        $data[ 'duty' ]               = $input[ 'post' ];
        $data[ 'real_name' ]          = $input[ 'name' ];
        $data[ 'phone' ]              = $input[ 'tel' ];
        $data[ 'certificate_number' ] = $input[ 'card' ];
        $data[ 'email' ]              = $input[ 'email' ];
        $data[ 'hiredate' ]           = $input[ 'apply_date' ];
        $check                        = object_to_array( DB::table( 'admin' )->where( [ "id" => $id ] )->first() );
        if (!empty( $check )){
            $res = DB::table( 'admin' )->where( [ "id" => $id ] )->update( $data );
            if ($res){
                return back()->with( 'success','修改成功' );
            }
            else{
                return back()->with( 'error','修改失败' );
            }
        }
        else{
            return back()->with( 'error','查无此人' );
        }
    }

    public function trademark()
    {
       /* $trademark=object_to_array(
            DB::table('progress')->where(array("progress.first_type_id"=>2))->
            join('second_type','progress.second_type_id','=','second_type.id')->
            join('product','progress.product_id','=','product.id')->
            join('progress_list','progress.progress_list_id','=','progress_list.id')->
            get(['product.applicant','second_type.name as second_type',
                'product.number','product.name','product.registration_time',
                'product.authority_time','product.quoter','progress_list.name as progress_list_type',
                'product.file','product.id'])
        );*/
        //dump($trademark);exit();
       // return view('generalAdmin/trademark')->with('data',$trademark);
        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->where(['first_type_id' => $this->trademark])
            ->orderBy('created_at', 'ASC')
            ->get();
        $progress = $this->getProgress($this->trademark);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->trademark))->
            get(['id', 'name'])
        );
        //统计全部项目个数
        $result=Db::table('product')
            ->where([
                ['deleted_at','=',null],
                ['first_type_id','=','2']
            ])
            ->count();
        //委托
        $result2=Db::table('progress')
            ->where([
                ['deleted_at','=',null],
                ['first_type_id','=','2']
            ])
            ->count();
       // dd($result2);
        $data = ['product', 'second_type', 'progress','result','result2'];
        //        dump($project);exit();
        return view( 'generalAdmin/trademark',compact($data) );
    }

    public function trademarkSearch()
    {
        $request = Request::all();
        $keywords="";
        if (isset($request['search'])) {
            $keywords = $request['search'];
        }
        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->where(['first_type_id' => $this->trademark])
            ->orderBy('created_at', 'ASC')
            ->get();
        $progress = $this->getProgress($this->trademark);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->trademark))->
            get(['id', 'name'])
        );
        $data = ['product', 'second_type', 'progress','keywords'];
        //        dump($project);exit();
        return view( 'generalAdmin/trademarkSearch',compact($data) );
    }

    public function trademarkCertainSearch()
    {
        $applicant = Request::input('applicant');
        $is_renew = Request::input('is_renew');
        $second_type = Request::input('second_type');
        $number = Request::input('number');
        $product_name = Request::input('product_name');
        $created_at = Request::input('created_at');
        $authority_time = Request::input('authority_time');
        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->join('product','product.id','=','progress.product_id')
            ->where(['progress.first_type_id' => $this->trademark])
            ->when($applicant, function ($query) use ($applicant) {
                return $query->where('product.applicant', $applicant);
            })
            ->when($is_renew, function ($query) use ($is_renew) {
                return $query->where('progress.is_renew', $is_renew);
            })
            ->when($second_type, function ($query) use ($second_type) {
                return $query->where('product.second_type_id', $second_type);
            })
            ->when($number, function ($query) use ($number) {
                return $query->where('product.number', $number);
            })
            ->when($product_name, function ($query) use ($product_name) {
                return $query->where('product.name', $product_name);
            })
            ->when($created_at, function ($query) use ($created_at) {
                return $query->where('product.created_at','like', '%'.$created_at.'%');
            })
            ->when($authority_time, function ($query) use ($authority_time) {
                return $query->where('product.authority_time','like', '%'.$authority_time.'%');
            })
            ->get();
        $progress = $this->getProgress($this->trademark);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->trademark))->
            get(['id', 'name'])
        );
        $data = ['product', 'second_type', 'progress','keywords'];
        //        dump($project);exit();
        return view( 'generalAdmin/trademarkSearch',compact($data) );
    }

    public function patent()
    {

        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->where(['first_type_id' => $this->patent])
            ->orderBy('created_at', 'ASC')
            ->get();
        $progress = $this->getProgress($this->patent);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->patent))->
            get(['id', 'name'])
        );
        //统计全部专利个数
        $result=Db::table('product')
            ->where([
                ['deleted_at','=',null],
                ['first_type_id','=','1']
            ])
            ->count();
        //委托
        $result2=Db::table('progress')
            ->where([
                ['deleted_at','=',null],
                ['first_type_id','=','1']
            ])
            ->count();
         //dd($result);
        $data = ['product', 'second_type', 'progress','result','result2'];
        //        dump($project);exit();
        return view( 'generalAdmin/patent',compact($data) );
    }
//    public function patentSearch()
//    {
//        $request = Request::all();
//        $keywords="";
//        if (isset($request['search'])) {
//            $keywords = $request['search'];
//        }
//        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
//            ->where(['first_type_id' => $this->patent])
//            ->orderBy('created_at', 'ASC')
//            ->get();
//        $progress = $this->getProgress($this->patent);
//        $second_type = object_to_array(
//            DB::table('second_type')->where(array("first_type_id" => $this->patent))->
//            get(['id', 'name'])
//        );
//        $data = ['product', 'second_type', 'progress','keywords'];
//        //        dump($project);exit();
//        return view( 'generalAdmin/patentSearch',compact($data) );
//    }

    public function patentCertainSearch()
    {
        $applicant = Request::input('applicant');
        $is_renew = Request::input('is_renew');
        $second_type = Request::input('second_type');
        $number = Request::input('number');
        $product_name = Request::input('product_name');
        $created_at = Request::input('created_at');
        $authority_time = Request::input('authority_time');
        $inventor = Request::input('inventor');
        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->join('product','product.id','=','progress.product_id')
            ->where(['progress.first_type_id' => $this->patent])
            ->orderBy('progress.created_at', 'ASC')
            ->when($applicant, function ($query) use ($applicant) {
                return $query->where('product.applicant', $applicant);
            })
            ->when($is_renew, function ($query) use ($is_renew) {
                return $query->where('progress.is_renew',$is_renew);
            })
            ->when($second_type, function ($query) use ($second_type) {
                return $query->where('product.second_type_id', $second_type);
            })
            ->when($number, function ($query) use ($number) {
                return $query->where('product.number', $number);
            })
            ->when($product_name, function ($query) use ($product_name) {
                return $query->where('product.name', $product_name);
            })
            ->when($created_at, function ($query) use ($created_at) {
                return $query->where('product.created_at','like', '%'.$created_at.'%');
            })
            ->when($authority_time, function ($query) use ($authority_time) {
                return $query->where('product.authority_time','like', '%'.$authority_time.'%');
            })
            ->when($inventor, function ($query) use ($inventor) {
                return $query->where('product.inventor',$inventor);
            })
            ->get();
        $progress = $this->getProgress($this->patent);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->patent))->
            get(['id', 'name'])
        );
        $data = ['product', 'second_type', 'progress'];
        //        dump($project);exit();
        return view( 'generalAdmin/patentSearch',compact($data) );
    }
    public function copyright()
    {

        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->where(['first_type_id' => $this->copyright])
            ->orderBy('created_at', 'ASC')
            ->get();
        $progress = $this->getProgress($this->copyright);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->copyright))->
            get(['id', 'name'])
        );
        //统计全部专利个数
        $result=Db::table('product')
            ->where([
                ['deleted_at','=',null],
                ['first_type_id','=','3']
            ])
            ->count();
        //委托
        $result2=Db::table('progress')
            ->where([
                ['deleted_at','=',null],
                ['first_type_id','=','3']
            ])
            ->count();
      //  dump($product);exit;
        $data = ['product', 'second_type', 'progress','result','result2'];
        //        dump($project);exit();
        return view( 'generalAdmin/copyright',compact($data) );
    }
//    public function copyrightSearch()
//    {
//        $request = Request::all();
//        $keywords="";
//        if (isset($request['search'])) {
//            $keywords = $request['search'];
//        }
//        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
//            ->where(['first_type_id' => $this->copyright])
//            ->orderBy('created_at', 'ASC')
//            ->get();
//        $progress = $this->getProgress($this->copyright);
//        $second_type = object_to_array(
//            DB::table('second_type')->where(array("first_type_id" => $this->copyright))->
//            get(['id', 'name'])
//        );
//        $data = ['product', 'second_type', 'progress','keywords'];
//        //        dump($project);exit();
//        return view( 'generalAdmin/copyrightSearch',compact($data) );
//    }

    public function copyrightCertainSearch()
    {
        $applicant = Request::input('applicant');
        $company = Request::input('company');
        $second_type = Request::input('second_type');
        $number = Request::input('number');
        $product_name = Request::input('product_name');
        $created_at = Request::input('created_at');
        $authority_time = Request::input('authority_time');
        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->join('product','product.id','=','progress.product_id')
            ->where(['progress.first_type_id' => $this->copyright])
            ->orderBy('progress.created_at', 'ASC')
            ->when($applicant, function ($query) use ($applicant) {
                return $query->where('product.applicant', $applicant);
            })
            ->when($company, function ($query) use ($company) {
                return $query->where('product.company', $company);
            })
            ->when($second_type, function ($query) use ($second_type) {
                return $query->where('product.second_type_id', $second_type);
            })
            ->when($number, function ($query) use ($number) {
                return $query->where('product.number', $number);
            })
            ->when($product_name, function ($query) use ($product_name) {
                return $query->where('product.name', $product_name);
            })
            ->when($created_at, function ($query) use ($created_at) {
                return $query->where('product.created_at','like', '%'.$created_at.'%');
            })
            ->when($authority_time, function ($query) use ($authority_time) {
                return $query->where('product.authority_time','like', '%'.$authority_time.'%');
            })
            ->get();
        $progress = $this->getProgress($this->copyright);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->copyright))->
            get(['id', 'name'])
        );
        $data = ['product', 'second_type', 'progress'];
        //        dump($project);exit();
        return view( 'generalAdmin/copyrightSearch',compact($data) );
    }
    public function project()
    {
        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->where(['first_type_id' => $this->project])
            ->orderBy('created_at', 'ASC')
            ->get();
        $progress = $this->getProgress($this->project);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->project))->
            get(['id', 'name'])
        );
        //统计全部专利个数
        $result=Db::table('product')
            ->where([
                ['deleted_at','=',null],
                ['first_type_id','=','4']
            ])
            ->count();
        //委托
        $result2=Db::table('progress')
            ->where([
                ['deleted_at','=',null],
                ['first_type_id','=','4']
            ])
            ->count();
        $data = ['product', 'second_type', 'progress','result','result2'];

        //        dump($project);exit();
        return view( 'generalAdmin/project',compact($data) );
    }
//    public function projectSearch()
//    {
//        $request = Request::all();
//        $keywords="";
//        if (isset($request['search'])) {
//            $keywords = $request['search'];
//        }
//        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
//            ->where(['first_type_id' => $this->project])
//            ->orderBy('created_at', 'ASC')
//            ->get();
//        $progress = $this->getProgress($this->project);
//        $second_type = object_to_array(
//            DB::table('second_type')->where(array("first_type_id" => $this->project))->
//            get(['id', 'name'])
//        );
//        $data = ['product', 'second_type', 'progress','keywords'];
//        return view( 'generalAdmin/projectSearch',compact($data) );
//    }

    public function projectCertainSearch()
    {
        $applicant = Request::input('applicant');
        $is_renew = Request::input('is_renew');
        $second_type = Request::input('second_type');
        $registration_time = Request::input('registration_time');
        $registration_area = Request::input('registration_area');
        $created_at = Request::input('created_at');
        $authority_time = Request::input('authority_time');
        $product = Progress::with(['Product', 'SecondType', 'FirstType'])
            ->join('product','product.id','=','progress.product_id')
            ->where(['progress.first_type_id' => $this->project])
            ->orderBy('progress.created_at', 'ASC')
            ->when($applicant, function ($query) use ($applicant) {
                return $query->where('product.applicant', $applicant);
            })
            ->when($is_renew, function ($query) use ($is_renew) {
                return $query->where('progress.is_renew', $is_renew);
            })
            ->when($second_type, function ($query) use ($second_type) {
                return $query->where('product.second_type_id', $second_type);
            })
            ->when($registration_time, function ($query) use ($registration_time) {
                return $query->where('product.registration_time','like', '%'.$registration_time.'%');
            })
            ->when($registration_area, function ($query) use ($registration_area) {
                return $query->where('product.registration_area', $registration_area);
            })
            ->when($created_at, function ($query) use ($created_at) {
                return $query->where('product.created_at','like', '%'.$created_at.'%');
            })
            ->when($authority_time, function ($query) use ($authority_time) {
                return $query->where('product.authority_time','like', '%'.$authority_time.'%');
            })
            ->get();
        $progress = $this->getProgress($this->project);
        $second_type = object_to_array(
            DB::table('second_type')->where(array("first_type_id" => $this->project))->
            get(['id', 'name'])
        );
        $data = ['product', 'second_type', 'progress','keywords'];
        return view( 'generalAdmin/projectSearch',compact($data) );
    }
    public function loginOut()
    {
        session()->flush();

        return Redirect::to( 'Admin/generalAdmin/login' );
    }
    /**
     * 文件压缩打包下载
     * @return string
     */
    public function ajxZip()
    {
        $request=Request::all();
        $pid=$request['id'];
        $item=Product::find($pid);
        $file=zipDownloads($item['user_id'],$pid);
        if(file_exists($file)){
            return json_return($file);
        }else{
            return json_return("0");
        }

    }
    public function getProgress($id)
    {
        $p = ota(FirstType::with(['ProgressList'])
            ->where(['id' => $id])
            ->first());
        $progress = [];
        foreach ($p['progress_list'] as $k => $v) {
            $item = [];
            $item['name'] = $v['name'];
            $item['flag'] = $v['progress_state'];
            $progress[$v['id']] = $item;
        }
        return $progress;
    }

    public function noAccount()
    {
        $result=Db::table('progress')->where('state',0)->count();
        $data=['result'];
        return view( 'generalAdmin/index',compact(['count']));
    }

    public function IpSell()//处理ip出售方法
    {
        $product=Ipsell::with(['FirstType','SecondType'])
            ->paginate($this->pageNum);
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $data = ['product','first_type'];
        //dump($product);exit;
        return view('generalAdmin/ipSell', compact($data));
    }

    public function IpSellProgress()//交易出售修改进度方法
    {
        $input = Request::all();
        $id = $input['hide_id'];
//        dd($id);
        $data['progress'] = $input['edit_progress'];
        if (!empty($data)) {
            $res = DB::table('ipsell')->where(array("id" => $id))->update($data);
            if ($res) {
                return back()->with('success', '修改成功');
            } else {
                return back()->with('error', '修改失败');
            }
        } else {
            return back()->with('error', '暂无数据');
        }
    }

//    public function IpSellCertain()
//    {
//        $request = Request::all();
//        $first_type = $request['first_type'];
//        $second_type = $request['second_type'];
//        $name = $request['name'];
//        $created_at = $request['created_at'];
//        $progress = $request['progress'];
//        $arr_province = ["不填","专利","商标","版权","项目"];
//        $arr_second_type=config('config.arr_second_type');
//        for ($j = 0;$j < sizeof($arr_second_type);$j++){
//            if ($second_type == $arr_second_type[$j]){
//                if ($j==0)
//                    $j='';
//                break;
//            }
//        }
//        for ($i = 0;$i < 5;$i++){
//            if ($first_type == $arr_province[$i]){
//                if ($i==0)
//                    $i='';
//                break;
//            }
//        }
//        // dd($request);
//        $product=Ipsell::with(['FirstType','SecondType'])
//            ->when($i, function ($query) use ($i) {
//                return $query->where('first_type', $i);
//            })
//            ->when($j, function ($query) use ($j) {
//                return $query->where('second_type', $j);
//            })
//            ->when($name, function ($query) use ($name) {
//                return $query->where('product_name', $name);
//            })
//            ->when($created_at, function ($query) use ($created_at) {
//                return $query->where('created_at','like','%'.$created_at.'%');
//            })
//            ->when($progress, function ($query) use ($progress) {
//                return $query->where('progress', $progress);
//            })
//            ->paginate($this->pageNum);
//        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
//        $data = ['product','first_type'];
//        return view('user/ip/IpPurchaseSearch', compact($data));
//    }

}